<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

	<title>Class: LocalVirtualNode</title>

	<link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />

	<script src="./js/jquery.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/thickbox-compressed.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/quicksearch.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/darkfish.js" type="text/javascript"
		charset="utf-8"></script>

</head>
<body class="class">

	<div id="metadata">
		<div id="home-metadata">
			<div id="home-section" class="section">
        <h3 class="section-header">
          <a href="./index.html">Home</a>
          <a href="./index.html#classes">Classes</a>
          <a href="./index.html#methods">Methods</a>
        </h3>
			</div>
		</div>

		<div id="file-metadata">
			<div id="file-list-section" class="section">
				<h3 class="section-header">In Files</h3>
				<div class="section-body">
					<ul>
					
						<li><a href="./localvirtualnode_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
							class="thickbox" title="localvirtualnode.rb">localvirtualnode.rb</a></li>
					
					</ul>
				</div>
			</div>

			
		</div>

		<div id="class-metadata">

			<!-- Parent Class -->
			
			<div id="parent-class-section" class="section">
				<h3 class="section-header">Parent</h3>
				
				<p class="link"><a href="AbstractVirtualNode.html">AbstractVirtualNode</a></p>
				
			</div>
			

			<!-- Namespace Contents -->
			

			<!-- Method Quickref -->
			
			<div id="method-list-section" class="section">
				<h3 class="section-header">Methods</h3>
				<ul class="link-list">
					
					<li><a href="#method-c-new">::new</a></li>
					
					<li><a href="#method-i-check_predecessors">#check_predecessors</a></li>
					
					<li><a href="#method-i-compact_succs">#compact_succs</a></li>
					
					<li><a href="#method-i-create">#create</a></li>
					
					<li><a href="#method-i-find_successor">#find_successor</a></li>
					
					<li><a href="#method-i-fix_fingers">#fix_fingers</a></li>
					
					<li><a href="#method-i-get_closest_preceding_node">#get_closest_preceding_node</a></li>
					
					<li><a href="#method-i-get_item">#get_item</a></li>
					
					<li><a href="#method-i-get_pred_list">#get_pred_list</a></li>
					
					<li><a href="#method-i-get_succ_list">#get_succ_list</a></li>
					
					<li><a href="#method-i-insert_item">#insert_item</a></li>
					
					<li><a href="#method-i-join">#join</a></li>
					
					<li><a href="#method-i-leave">#leave</a></li>
					
					<li><a href="#method-i-notify">#notify</a></li>
					
					<li><a href="#method-i-ping">#ping</a></li>
					
					<li><a href="#method-i-remove_item">#remove_item</a></li>
					
					<li><a href="#method-i-stabilize">#stabilize</a></li>
					
					<li><a href="#method-i-start_stabilize_threads">#start_stabilize_threads</a></li>
					
					<li><a href="#method-i-update_succ_list">#update_succ_list</a></li>
					
				</ul>
			</div>
			

			<!-- Included Modules -->
			
		</div>

		<div id="project-metadata">
			
			

			<div id="classindex-section" class="section project-section">
				<h3 class="section-header">Class Index
					<span class="search-toggle"><img src="./images/find.png"
						height="16" width="16" alt="[+]"
						title="show/hide quicksearch" /></span></h3>
				<form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
				<fieldset>
					<legend>Quicksearch</legend>
					<input type="text" name="quicksearch" value=""
						class="quicksearch-field" />
				</fieldset>
				</form>

				<ul class="link-list">
				
					<li><a href="./Chorb.html">Chorb</a></li>
				
					<li><a href="./Chorb/Chord.html">Chorb::Chord</a></li>
				
					<li><a href="./AbstractVirtualNode.html">AbstractVirtualNode</a></li>
				
					<li><a href="./Key.html">Key</a></li>
				
					<li><a href="./ListenerVirtualNode.html">ListenerVirtualNode</a></li>
				
					<li><a href="./LocalVirtualNode.html">LocalVirtualNode</a></li>
				
					<li><a href="./RemoteVirtualNode.html">RemoteVirtualNode</a></li>
				
				</ul>
				<div id="no-class-search-results" style="display: none;">No matching classes.</div>
			</div>

			
		</div>
	</div>

	<div id="documentation">
		<h1 class="class">LocalVirtualNode</h1>

		<div id="description">
			<p>
Implementacao de <a href="AbstractVirtualNode.html">AbstractVirtualNode</a>
local. Representa um virtual node local do Chord.
</p>

		</div>

		<!-- Constants -->
		
		<div id="constants-list" class="section">
			<h3 class="section-header">Constants</h3>
			<dl>
			
				<dt><a name="STABILIZE_THREAD_DELAY">STABILIZE_THREAD_DELAY</a></dt>
				
				<dd class="description"></dd>
				
			
				<dt><a name="FIX_FINGERS_THREAD_DELAY">FIX_FINGERS_THREAD_DELAY</a></dt>
				
				<dd class="description"></dd>
				
			
				<dt><a name="CHECK_PREDECESSORS_THREAD_DELAY">CHECK_PREDECESSORS_THREAD_DELAY</a></dt>
				
				<dd class="description"></dd>
				
			
			</dl>
		</div>
		

		<!-- Attributes -->
		
		<div id="attribute-method-details" class="method-section section">
			<h3 class="section-header">Attributes</h3>

			
			<div id="finger-table-attribute-method" class="method-detail">
				<a name="finger_table"></a>
				
				<div class="method-heading attribute-method-heading">
					<span class="method-name">finger_table</span><span
						class="attribute-access-type">[R]</span>
				</div>

				<div class="method-description">
				
				
				
				</div>
			</div>
			
			<div id="host-attribute-method" class="method-detail">
				<a name="host"></a>
				
				<a name="host="></a>
				
				<div class="method-heading attribute-method-heading">
					<span class="method-name">host</span><span
						class="attribute-access-type">[RW]</span>
				</div>

				<div class="method-description">
				
				
				
				</div>
			</div>
			
		</div>
		

		<!-- Methods -->
		
		<div id="public-class-method-details" class="method-section section">
			<h3 class="section-header">Public Class Methods</h3>

		
			<div id="new-method" class="method-detail ">
				<a name="method-c-new"></a>

				<div class="method-heading">
				
					<span class="method-name">new</span><span
						class="method-args">(id, host, port, num_successors, callback)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Construtor
</p>
<ul>
<li><p>
<tt>id</tt> - Instancia de <a href="Key.html">Key</a>, representa chave
unica desse virtual node.
</p>
</li>
<li><p>
<tt>host</tt> - Host sobre o qual esta rodando.
</p>
</li>
<li><p>
<tt>port</tt> - Porta de esculta.
</p>
</li>
<li><p>
<tt>num_successors</tt> - Numero de sucessores a serem mantidos.
</p>
</li>
<li><p>
<tt>callback</tt> - Metodo a ser chamado quando a lista de sucessores
mudar, seja
</p>
<pre>
             devido a entrada ou saida de no's.</pre>
</li>
</ul>
					

					
					<div class="method-source-code"
						id="new-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 22</span>
22:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">id</span>, <span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-identifier">num_successors</span>, <span class="ruby-identifier">callback</span>)
23:     <span class="ruby-ivar">@node_id</span> = <span class="ruby-identifier">id</span>
24:     <span class="ruby-ivar">@port</span> = <span class="ruby-identifier">port</span>
25:     <span class="ruby-ivar">@host</span> = <span class="ruby-identifier">host</span>
26:     <span class="ruby-ivar">@listener_node</span> = <span class="ruby-constant">ListenerVirtualNode</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
27:     <span class="ruby-ivar">@server</span> = <span class="ruby-constant">DRb</span><span class="ruby-operator">::</span><span class="ruby-constant">DRbServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;druby://#{host}:#{port}&quot;</span>, <span class="ruby-ivar">@listener_node</span>)
28:     <span class="ruby-ivar">@threads</span> = []
29:     <span class="ruby-ivar">@sucessors</span> = []
30:     <span class="ruby-ivar">@num_successors</span> = <span class="ruby-identifier">num_successors</span>
31:     <span class="ruby-ivar">@predecessor</span> = <span class="ruby-keyword kw">nil</span>
32:     <span class="ruby-ivar">@finger_table</span> = []
33:     <span class="ruby-ivar">@alive</span> = <span class="ruby-keyword kw">true</span>
34:     <span class="ruby-ivar">@next</span> = <span class="ruby-value">0</span>
35:     <span class="ruby-ivar">@callback</span> = <span class="ruby-identifier">callback</span>
36:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
		</div>
	
		<div id="public-instance-method-details" class="method-section section">
			<h3 class="section-header">Public Instance Methods</h3>

		
			<div id="check-predecessors-method" class="method-detail ">
				<a name="method-i-check_predecessors"></a>

				<div class="method-heading">
				
					<span class="method-name">check_predecessors</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Checa o predecessor. Se ele nao responder o predecessor atual e
descartado.
</p>
					

					
					<div class="method-source-code"
						id="check-predecessors-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 205</span>
205:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">check_predecessors</span>
206:     <span class="ruby-keyword kw">begin</span>
207:       <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@predecessor</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">and</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@predecessor</span>.<span class="ruby-identifier">ping</span>
208:         <span class="ruby-ivar">@predecessor</span> = <span class="ruby-keyword kw">nil</span>
209:       <span class="ruby-keyword kw">end</span>
210:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
211:       <span class="ruby-ivar">@predecessor</span> = <span class="ruby-keyword kw">nil</span>
212:     <span class="ruby-keyword kw">end</span>
213:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="compact-succs-method" class="method-detail ">
				<a name="method-i-compact_succs"></a>

				<div class="method-heading">
				
					<span class="method-name">compact_succs</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="compact-succs-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 182</span>
182:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">compact_succs</span>
183:     <span class="ruby-keyword kw">begin</span>
184:       <span class="ruby-identifier">succs</span> = []
185:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">compact!</span>
186:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">delete_if</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">succ</span><span class="ruby-operator">|</span> <span class="ruby-operator">!</span><span class="ruby-identifier">succ</span>.<span class="ruby-identifier">ping</span>}
187:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">uniq!</span>
188:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">succ</span><span class="ruby-operator">|</span>
189:         <span class="ruby-identifier">succs</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">succ</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">succ</span>.<span class="ruby-identifier">node_id</span> <span class="ruby-operator">!=</span> <span class="ruby-ivar">@node_id</span>)
190:       <span class="ruby-keyword kw">end</span>
191:       <span class="ruby-ivar">@successors</span> = <span class="ruby-identifier">succs</span>
192:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
193:       <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;COMPACTSUCCS : #{e.class} : #{e.message}&quot;</span>
194:     <span class="ruby-keyword kw">end</span>
195:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="create-method" class="method-detail ">
				<a name="method-i-create"></a>

				<div class="method-heading">
				
					<span class="method-name">create</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
O primeiro virtual node a ser instanciado precisa ter esse metodo chamado
pois ele sera o ponto de partida para entrada dos outros no&#8217;s.
</p>
					

					
					<div class="method-source-code"
						id="create-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 70</span>
70:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create</span>
71:     <span class="ruby-ivar">@predecessor</span> = <span class="ruby-keyword kw">nil</span>
72:     <span class="ruby-comment cmt">#TODO Setar esse protocol como constante depois</span>
73:     <span class="ruby-comment cmt">#Tem que criar um remoto pois sera passado pela get_successors</span>
74:     <span class="ruby-ivar">@successors</span> = [<span class="ruby-constant">RemoteVirtualNode</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'druby'</span>, <span class="ruby-ivar">@host</span>, <span class="ruby-ivar">@port</span>)]
75:     <span class="ruby-identifier">start_stabilize_threads</span>
76:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="find-successor-method" class="method-detail ">
				<a name="method-i-find_successor"></a>

				<div class="method-heading">
				
					<span class="method-name">find_successor</span><span
						class="method-args">(id)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Implementacao efetiva do find_sucessor.
</p>
<p>
Se da em duas etapas:
</p>
<ol>
<li><p>
Checa atraves da lista de sucessores.
</p>
</li>
<li><p>
Acha o no mais proximo atraves da finger table e chama recursivamente.
</p>
</li>
</ol>
					

					
					<div class="method-source-code"
						id="find-successor-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 43</span>
43:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">find_successor</span>(<span class="ruby-identifier">id</span>)
44:     <span class="ruby-comment cmt"># Checa proximos</span>
45:     <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">successor</span><span class="ruby-operator">|</span>
46:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">id</span>.<span class="ruby-identifier">is_in_range</span>(<span class="ruby-ivar">@node_id</span>, <span class="ruby-identifier">successor</span>.<span class="ruby-identifier">node_id</span>, <span class="ruby-keyword kw">false</span>)
47:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">successor</span>
48:       <span class="ruby-keyword kw">end</span>
49:     <span class="ruby-keyword kw">end</span>
50:     <span class="ruby-keyword kw">begin</span>    
51:       <span class="ruby-identifier">get_closest_preceding_node</span>(<span class="ruby-identifier">id</span>).<span class="ruby-identifier">find_successor</span>(<span class="ruby-identifier">id</span>)
52:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
53:       <span class="ruby-comment cmt">#puts &quot;find -&gt; get closest&quot;</span>
54:       <span class="ruby-comment cmt">#puts &quot;#{e.class} :#{e.message}&quot;</span>
55:     <span class="ruby-keyword kw">end</span>
56:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="fix-fingers-method" class="method-detail ">
				<a name="method-i-fix_fingers"></a>

				<div class="method-heading">
				
					<span class="method-name">fix_fingers</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Atualiza a entrada na finger table de acordo com o valor de next.
</p>
					

					
					<div class="method-source-code"
						id="fix-fingers-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 198</span>
198:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">fix_fingers</span>
199:     <span class="ruby-ivar">@next</span> = <span class="ruby-ivar">@next</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">10</span> <span class="ruby-operator">?</span> <span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@next</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
200:     <span class="ruby-ivar">@finger_table</span>[<span class="ruby-ivar">@next</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>] = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">find_successor</span>(<span class="ruby-ivar">@node_id</span>.<span class="ruby-operator">+</span>(<span class="ruby-value">2</span><span class="ruby-operator">**</span>(<span class="ruby-ivar">@next</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>)))
201:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="get-item-method" class="method-detail ">
				<a name="method-i-get_item"></a>

				<div class="method-heading">
				
					<span class="method-name">get_item</span><span
						class="method-args">(key)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="get-item-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 96</span>
96:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_item</span>(<span class="ruby-identifier">key</span>)
97:     <span class="ruby-comment cmt">#TODO Implementar.</span>
98:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="get-pred-list-method" class="method-detail ">
				<a name="method-i-get_pred_list"></a>

				<div class="method-heading">
				
					<span class="method-name">get_pred_list</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Retorna a lista de predecessores.
</p>
					

					
					<div class="method-source-code"
						id="get-pred-list-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 92</span>
92:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_pred_list</span>
93:     <span class="ruby-ivar">@predecessor</span>
94:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="get-succ-list-method" class="method-detail ">
				<a name="method-i-get_succ_list"></a>

				<div class="method-heading">
				
					<span class="method-name">get_succ_list</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Retorna a lista de sucessores.
</p>
					

					
					<div class="method-source-code"
						id="get-succ-list-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 87</span>
87:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_succ_list</span>
88:     <span class="ruby-ivar">@successors</span>
89:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="insert-item-method" class="method-detail ">
				<a name="method-i-insert_item"></a>

				<div class="method-heading">
				
					<span class="method-name">insert_item</span><span
						class="method-args">(key, value)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="insert-item-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 100</span>
100:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">insert_item</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)
101:     <span class="ruby-comment cmt">#TODO Implementar.</span>
102:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="join-method" class="method-detail ">
				<a name="method-i-join"></a>

				<div class="method-heading">
				
					<span class="method-name">join</span><span
						class="method-args">(node)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Entra no Chord ring atraves do no&#8217; passado.
</p>
					

					
					<div class="method-source-code"
						id="join-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 79</span>
79:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">join</span>(<span class="ruby-identifier">node</span>)
80:     <span class="ruby-ivar">@predecessor</span> = <span class="ruby-keyword kw">nil</span>
81:     <span class="ruby-ivar">@successors</span> = [<span class="ruby-identifier">node</span>.<span class="ruby-identifier">find_successor</span>(<span class="ruby-ivar">@node_id</span>)]
82:     <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">concat</span>(<span class="ruby-ivar">@successors</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">get_succ_list</span>())
83:     <span class="ruby-identifier">start_stabilize_threads</span>
84:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="leave-method" class="method-detail ">
				<a name="method-i-leave"></a>

				<div class="method-heading">
				
					<span class="method-name">leave</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Deixa o Chord ring voluntariamente.
</p>
					

					
					<div class="method-source-code"
						id="leave-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 216</span>
216:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">leave</span>
217:     <span class="ruby-ivar">@alive</span> = <span class="ruby-keyword kw">false</span>
218:     <span class="ruby-ivar">@server</span>.<span class="ruby-identifier">stop_service</span>()
219:     <span class="ruby-ivar">@threads</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">thread</span><span class="ruby-operator">|</span> <span class="ruby-identifier">thread</span>.<span class="ruby-identifier">join</span>}
220:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="notify-method" class="method-detail ">
				<a name="method-i-notify"></a>

				<div class="method-heading">
				
					<span class="method-name">notify</span><span
						class="method-args">(node)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Chamado quando um no&#8217; entra no Chord ring e deseja notificar seu
novo sucessor da sua presenca. Notify soh eh chamado pelo pred com os
succs ja atualizados.
</p>
					

					
					<div class="method-source-code"
						id="notify-source">
<pre>
    <span class="ruby-comment cmt"># File localvirtualnode.rb, line 61</span>
61:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">notify</span>(<span class="ruby-identifier">node</span>)
62:     <span class="ruby-keyword kw">if</span> (<span class="ruby-ivar">@predecessor</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">or</span> (<span class="ruby-ivar">@predecessor</span>.<span class="ruby-identifier">ping</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">node_id</span>.<span class="ruby-identifier">is_in_range</span>(<span class="ruby-ivar">@predecessor</span>.<span class="ruby-identifier">node_id</span>, <span class="ruby-ivar">@node_id</span>, <span class="ruby-keyword kw">false</span>, <span class="ruby-keyword kw">false</span>)))
63:       <span class="ruby-ivar">@predecessor</span> = <span class="ruby-identifier">node</span>
64:       <span class="ruby-comment cmt">#@predecessor.update_succ_list(@successors)</span>
65:     <span class="ruby-keyword kw">end</span>
66:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="ping-method" class="method-detail ">
				<a name="method-i-ping"></a>

				<div class="method-heading">
				
					<span class="method-name">ping</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Retorna se ainda esta vivo.
</p>
					

					
					<div class="method-source-code"
						id="ping-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 223</span>
223:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">ping</span>
224:     <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@alive</span>
225:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="remove-item-method" class="method-detail ">
				<a name="method-i-remove_item"></a>

				<div class="method-heading">
				
					<span class="method-name">remove_item</span><span
						class="method-args">(key)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="remove-item-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 104</span>
104:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">remove_item</span>(<span class="ruby-identifier">key</span>)
105:     <span class="ruby-comment cmt">#TODO Implementar.</span>
106:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="stabilize-method" class="method-detail ">
				<a name="method-i-stabilize"></a>

				<div class="method-heading">
				
					<span class="method-name">stabilize</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Estabiliza o a lista de sucessores. Checa se os sucessores que estao vivos
e determina se o sucessor imediato esta correto.
</p>
					

					
					<div class="method-source-code"
						id="stabilize-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 111</span>
111:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">stabilize</span>
112:     
113:     <span class="ruby-keyword kw">begin</span>
114:       <span class="ruby-identifier">succs</span> = []
115:       
116:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">node</span><span class="ruby-operator">|</span>
117:         <span class="ruby-keyword kw">begin</span>     
118:           <span class="ruby-identifier">succs</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">node</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">node</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">ping</span>
119:         <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
120:           <span class="ruby-comment cmt">#puts &quot;NAO PINGOU&quot;</span>
121:           <span class="ruby-comment cmt"># por algum motivo o ping ta lancando uma excecao</span>
122:           <span class="ruby-comment cmt"># e quando eu coloco esse begin rescue funciona mesmo lancando a excecao</span>
123:         <span class="ruby-keyword kw">end</span>
124:       <span class="ruby-keyword kw">end</span>
125:       
126:       <span class="ruby-ivar">@successors</span> = (<span class="ruby-identifier">succs</span> <span class="ruby-operator">!=</span> [] <span class="ruby-operator">?</span> <span class="ruby-identifier">succs</span> <span class="ruby-operator">:</span> [<span class="ruby-constant">RemoteVirtualNode</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'druby'</span>, <span class="ruby-ivar">@host</span>, <span class="ruby-ivar">@port</span>)])
127:       <span class="ruby-identifier">node</span> = <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">get_pred_list</span>
128:     
129:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">node</span>
130:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">node_id</span>.<span class="ruby-identifier">is_in_range</span>(<span class="ruby-ivar">@node_id</span>, <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">node_id</span>, <span class="ruby-keyword kw">false</span>, <span class="ruby-keyword kw">false</span>)
131:           <span class="ruby-ivar">@successors</span> = [<span class="ruby-identifier">node</span>]
132:           <span class="ruby-identifier">aux_successors</span> = <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">get_succ_list</span>()
133:           <span class="ruby-identifier">update_succ_list</span>(<span class="ruby-identifier">aux_successors</span>)
134:           <span class="ruby-ivar">@predecessor</span>.<span class="ruby-identifier">update_succ_list</span>(<span class="ruby-ivar">@successors</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@predecessor</span>
135:         <span class="ruby-keyword kw">end</span>
136:       <span class="ruby-keyword kw">end</span>
137:       
138:       <span class="ruby-ivar">@predecessor</span>.<span class="ruby-identifier">update_succ_list</span>(<span class="ruby-ivar">@successors</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@predecessor</span>
139:       <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">notify</span>(<span class="ruby-keyword kw">self</span>)
140:       <span class="ruby-comment cmt">#@successors.uniq!</span>
141:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
142:       <span class="ruby-comment cmt">#puts &quot;Stabilize -&gt; notify&quot;</span>
143:       <span class="ruby-comment cmt">#puts &quot;#{e.class} :#{e.message}&quot;</span>
144:       <span class="ruby-comment cmt">#puts @node_id</span>
145:       <span class="ruby-comment cmt">#puts &quot;------------------------------------------------&quot;</span>
146:     <span class="ruby-keyword kw">end</span>
147:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="update-succ-list-method" class="method-detail ">
				<a name="method-i-update_succ_list"></a>

				<div class="method-heading">
				
					<span class="method-name">update_succ_list</span><span
						class="method-args">(successors)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Atualiza a lista de sucessores com base na lista passada.
</p>
					

					
					<div class="method-source-code"
						id="update-succ-list-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 150</span>
150:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_succ_list</span>(<span class="ruby-identifier">successors</span>)
151:     
152:     <span class="ruby-identifier">successors</span> = <span class="ruby-identifier">successors</span>[<span class="ruby-value">0</span><span class="ruby-operator">...</span>(<span class="ruby-ivar">@num_successors</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>)] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">successors</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-ivar">@num_successors</span>
153:     
154:     <span class="ruby-identifier">node_ids_curr</span> = []
155:     <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
156:       <span class="ruby-identifier">node_ids_curr</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">node_id</span>.<span class="ruby-identifier">key</span>
157:     <span class="ruby-keyword kw">end</span>
158:     
159:     <span class="ruby-ivar">@successors</span> =  [<span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">first</span>]
160:     <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">concat</span>(<span class="ruby-identifier">successors</span>)
161:     <span class="ruby-identifier">compact_succs</span>
162:     
163:     <span class="ruby-identifier">node_ids_fut</span> = []
164:     <span class="ruby-ivar">@successors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
165:       <span class="ruby-identifier">node_ids_fut</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">node_id</span>.<span class="ruby-identifier">key</span>
166:     <span class="ruby-keyword kw">end</span>
167:     
168:     <span class="ruby-identifier">changed</span> = <span class="ruby-keyword kw">false</span>
169:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">node_ids_curr</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">node_ids_fut</span>
170:       <span class="ruby-identifier">changed</span> = <span class="ruby-keyword kw">true</span>
171:     <span class="ruby-keyword kw">end</span>
172:     
173:     <span class="ruby-ivar">@callback</span>.<span class="ruby-identifier">call</span>(<span class="ruby-ivar">@node_id</span>, <span class="ruby-ivar">@successors</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">changed</span>
174:     
175:     <span class="ruby-comment cmt"># @successors =  [@successors.first]</span>
176:     <span class="ruby-comment cmt"># successors = successors[0...(@num_successors-1)] if successors.length &gt;= @num_successors</span>
177:     <span class="ruby-comment cmt"># @successors.concat(successors)</span>
178:     <span class="ruby-comment cmt"># compact_succs</span>
179:     <span class="ruby-comment cmt"># @callback.call</span>
180:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
		</div>
	
		<div id="private-instance-method-details" class="method-section section">
			<h3 class="section-header">Private Instance Methods</h3>

		
			<div id="get-closest-preceding-node-method" class="method-detail ">
				<a name="method-i-get_closest_preceding_node"></a>

				<div class="method-heading">
				
					<span class="method-name">get_closest_preceding_node</span><span
						class="method-args">(id)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Consulta a finger table para determinar o no&#8217; mais proximo de acordo
com id passado.
</p>
					

					
					<div class="method-source-code"
						id="get-closest-preceding-node-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 271</span>
271:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_closest_preceding_node</span>(<span class="ruby-identifier">id</span>)
272:     <span class="ruby-ivar">@finger_table</span>.<span class="ruby-identifier">reverse</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">remote_node</span><span class="ruby-operator">|</span>
273:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">remote_node</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">remote_node</span>.<span class="ruby-identifier">ping</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">remote_node</span>.<span class="ruby-identifier">node_id</span>.<span class="ruby-identifier">is_in_range</span>(<span class="ruby-ivar">@node_id</span>, <span class="ruby-identifier">id</span>)
274:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">remote_node</span>
275:       <span class="ruby-keyword kw">end</span>
276:     <span class="ruby-keyword kw">end</span>
277:     <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">self</span>
278:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
			<div id="start-stabilize-threads-method" class="method-detail ">
				<a name="method-i-start_stabilize_threads"></a>

				<div class="method-heading">
				
					<span class="method-name">start_stabilize_threads</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p>
Cria as threads de stabilizacao necessarias. Sao 3 threads:
</p>
<ol>
<li><p>
Thread que roda o metodo stabilize periodicamente.
</p>
</li>
<li><p>
Thread que roda o metodo <a
href="LocalVirtualNode.html#method-i-fix_fingers">fix_fingers</a>
periodicamente.
</p>
</li>
<li><p>
Thread que roda o metodo <a
href="LocalVirtualNode.html#method-i-check_predecessors">check_predecessors</a>
periodicamente.
</p>
</li>
</ol>
					

					
					<div class="method-source-code"
						id="start-stabilize-threads-source">
<pre>
     <span class="ruby-comment cmt"># File localvirtualnode.rb, line 233</span>
233:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">start_stabilize_threads</span>
234:     <span class="ruby-ivar">@threads</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
235:       <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@alive</span>
236:         <span class="ruby-keyword kw">begin</span>
237:           <span class="ruby-identifier">stabilize</span>
238:           <span class="ruby-identifier">sleep</span>(<span class="ruby-constant">STABILIZE_THREAD_DELAY</span>)
239:         <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
240:           <span class="ruby-comment cmt">#puts &quot;STABILIZE EXCEPTION&quot;</span>
241:           <span class="ruby-comment cmt">#puts &quot;#{e.class} : #{e.message}&quot;</span>
242:         <span class="ruby-keyword kw">end</span>
243:       <span class="ruby-keyword kw">end</span>
244:     <span class="ruby-keyword kw">end</span>
245:     <span class="ruby-ivar">@threads</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
246:       <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@alive</span>
247:         <span class="ruby-keyword kw">begin</span>
248:           <span class="ruby-identifier">fix_fingers</span>
249:           <span class="ruby-identifier">sleep</span>(<span class="ruby-constant">FIX_FINGERS_THREAD_DELAY</span>)
250:         <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
251:           <span class="ruby-comment cmt">#puts &quot;FIX FINGERS EXCEPTION&quot;</span>
252:           <span class="ruby-comment cmt">#puts &quot;#{e.class} : #{e.message}&quot;</span>
253:         <span class="ruby-keyword kw">end</span>
254:       <span class="ruby-keyword kw">end</span>
255:     <span class="ruby-keyword kw">end</span>
256:     <span class="ruby-ivar">@threads</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
257:       <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@alive</span>
258:         <span class="ruby-keyword kw">begin</span>
259:           <span class="ruby-identifier">check_predecessors</span>
260:           <span class="ruby-identifier">sleep</span>(<span class="ruby-constant">CHECK_PREDECESSORS_THREAD_DELAY</span>)
261:         <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
262:           <span class="ruby-comment cmt">#puts &quot;CHECK PREDECESSORS EXCEPTION&quot;</span>
263:           <span class="ruby-comment cmt">#puts &quot;#{e.class} : #{e.message}&quot;</span>
264:         <span class="ruby-keyword kw">end</span>
265:       <span class="ruby-keyword kw">end</span>
266:     <span class="ruby-keyword kw">end</span>    
267:   <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				

				
			</div>

		
		</div>
	

	</div>


	<div id="rdoc-debugging-section-dump" class="debugging-section">
	
		<p>Disabled; run with --debug to generate this.</p>
	
	</div>

	<div id="validator-badges">
		<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
		<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
			Rdoc Generator</a> 1.1.6</small>.</p>
	</div>

</body>
</html>

